<?php
declare (strict_types=1);

namespace app\admin\controller\iot;

use app\admin\service\iot\DeviceApkService;
use app\BaseController;
use think\App;
use think\facade\Request;
use think\response\Json;

/**
 * 设备Apk管理
 */
class DeviceApk extends BaseController
{

    /**
     * 设备APK列表
     * @param DeviceApkService $deviceApkService
     * @return Json
     */
    public function deviceApkList(DeviceApkService $deviceApkService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['name'] = Request::param('name/s');
        $params['status'] = Request::param('status/d');
        $params['start_time'] = Request::param('start_time/d');
        $params['end_time'] = Request::param('end_time/d');
        return $deviceApkService->deviceApkList($params);
    }

    /**
     * 设备APK上传
     * @param DeviceApkService $deviceApkService
     * @return Json
     */
    public function deviceApkUpload(DeviceApkService $deviceApkService): Json
    {
        $file = request()->file('apk');
        if (!$file) {
            return jsonReturn(0, []);
        }
        //处理上传apk信息
        return $deviceApkService->deviceApkUpload($file);
    }

    /**
     * 设备APK添加
     * @param DeviceApkService $deviceApkService
     * @return Json
     */
    public function deviceApkAdd(DeviceApkService $deviceApkService): Json
    {
        $params['name'] = Request::param('name/s');
        $params['version'] = Request::param('version/s');
        $params['type'] = Request::param('type/d');
        $params['src'] = Request::param('src/s');
        $params['size'] = Request::param('size/d');
        $params['remark'] = Request::param('remark/s', '');
        return $deviceApkService->deviceApkAdd($params);
    }

    /**
     * 设备APK删除
     * @param DeviceApkService $deviceApkService
     * @return Json
     */
    public function deviceApkDel(DeviceApkService $deviceApkService): Json
    {
        $id = Request::param('id/d', 0);
        return $deviceApkService->deviceApkDel($id);
    }

    /**
     * 设备APK上下架
     * @param DeviceApkService $deviceApkService
     * @return Json
     */
    public function deviceApkStatus(DeviceApkService $deviceApkService): Json
    {
        $id = Request::param('id/d', 0);
        $status = Request::param('status/d', 0);
        return $deviceApkService->deviceApkStatus($id, $status);
    }

    /**
     * 设备APK发布
     * @param DeviceApkService $deviceApkService
     * @return Json
     */
    public function deviceApkRelease(DeviceApkService $deviceApkService): Json
    {
        $id = Request::param('id/d', 0);
        $mac_ids = Request::param('mac_ids/a', []);
        return $deviceApkService->deviceApkRelease($id, $mac_ids);
    }

    /**
     * 设备APK日志列表
     * @param DeviceApkService $deviceApkService
     * @return Json
     */
    public function deviceApkLogList(DeviceApkService $deviceApkService): Json
    {
        $id = Request::param('id/d', 0);
        $page = Request::param('page/d', 1);
        $limit = Request::param('limit/d', 10);
        return $deviceApkService->deviceApkLogList($id, $page, $limit);
    }


}
